package com.xinke.edu.mypetadoption.fragment2;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.xinke.edu.mypetadoption.Activity.ChangePassword;
import com.xinke.edu.mypetadoption.Activity.Login_Activity;
import com.xinke.edu.mypetadoption.R;
import com.xinke.edu.mypetadoption.databinding.FragmentSzBinding;
import com.xinke.edu.mypetadoption.databinding.FragmentTjBinding;
import com.xinke.edu.mypetadoption.entity.MerchantInfoDTO;
import com.xinke.edu.mypetadoption.entity.OrdersUserProductDTO;
import com.xinke.edu.mypetadoption.entity.ResultBody;
import com.xinke.edu.mypetadoption.util.RetrofitUtil;
import com.xinke.edu.mypetadoption.util.SPUtils;

import java.math.BigDecimal;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;


public class SzFragment extends Fragment {

    FragmentSzBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = FragmentSzBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        loaddata();

    }



    private void loaddata(){

        //获取登录用户id（确保与Login_Activity中存储的ID完全一致）
        int userId = SPUtils.getInstance(requireContext()).getInt("userId", 1);

        Log.d("SzFragment", "使用的用户ID: " + userId);
        
        RetrofitUtil.getApi().getMerchantInfo(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody<MerchantInfoDTO>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {}

                    @Override
                    public void onNext(@NonNull ResultBody<MerchantInfoDTO> result) {

                        MerchantInfoDTO info = result.getData();
                        if (info != null) {

                            ImageView img = getView().findViewById(R.id.dptx);
                            Glide.with(getContext()).load(info.getAvatar()).into(img);

                            binding.dpmc.setText(info.getMerchantName());
                            binding.dpms.setText(info.getMerchantName());
                            binding.dpdh.setText(info.getContactPhone());
                            binding.dplx.setText(info.getContactPerson());
                            binding.dpsj.setText(info.getJoinTime());

                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(requireContext(), "加载失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {}
                });

        // 设置"修改密码"区域的点击事件（XML ID: ChangePassword - 修改密码功能入口区域）
        binding.ChangePassword.setOnClickListener(v ->{
            if (getActivity() != null){
                // 获取当前用户ID并转换为int类型
                // 直接获取int类型的用户ID，无需类型转换
                //int userId = SPUtils.getInstance(getActivity()).getInt("userId", 0);
                Intent intent = new Intent(getActivity(), ChangePassword.class);
                // 传递用户ID到ChangePassword
                intent.putExtra("userId", userId);
                startActivity(intent);
            }
        });

        // 设置"退出登录"区域的点击事件（XML ID: LogOut - 退出登录功能入口区域）
        binding.exit.setOnClickListener(v ->{
            if (getActivity() != null){
                //跳回登录界面
                Intent intent = new Intent(getActivity(), Login_Activity.class);
                //清除当前activity的所有页面，避免点击返回出现其他页面
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                //关闭fragment
                getActivity().finish();
            }
        });

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }



}